package com.dayu.recovery.service.impl;

import java.util.List;

import com.dayu.common.core.domain.entity.SysUser;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.common.utils.StringUtils;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.SalePriceLogMapper;
import com.dayu.recovery.domain.SalePriceLog;
import com.dayu.recovery.service.ISalePriceLogService;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 预售价日志Service业务层处理
 *
 * @author HK
 * @date 2022-03-21
 */
@Service
public class SalePriceLogServiceImpl implements ISalePriceLogService
{
    @Resource
    private SalePriceLogMapper salePriceLogMapper;
    @Resource
    private RecoveryGoodsMapper goodsMapper;

    /**
     * 查询预售价日志列表
     *
     * @param salePriceLog 预售价日志
     * @return 预售价日志
     */
    @Override
    public List<SalePriceLog> selectSalePriceLogList(SalePriceLog salePriceLog)
    {
        return salePriceLogMapper.selectSalePriceLogList(salePriceLog);
    }

    /**
     * 新增预售价日志
     *
     * @param salePriceLog 预售价日志
     * @return 结果
     */
    @Override
    public int insertSalePriceLog(SalePriceLog salePriceLog)
    {
        if(StringUtils.isEmpty(salePriceLog.getGoodsId())) {
            throw new RuntimeException("商品编号不能为空！");
        }
        if(salePriceLog.getPlatform() == null) {
            throw new RuntimeException("请选择平台！");
        }
        if(salePriceLog.getPrice() == null) {
            throw new RuntimeException("请输入价格！");
        }

        RecoveryGoods goods = goodsMapper.selectRecoveryGoodsById(salePriceLog.getGoodsId());
        if(ObjectUtils.isEmpty(goods)) {
            throw new RuntimeException("请填写正确的编码！");
        }

        //修改以前的数据 为历史
        SalePriceLog editLog = new SalePriceLog();
        editLog.setGoodsId(salePriceLog.getGoodsId());
        editLog.setPlatform(salePriceLog.getPlatform());
        salePriceLogMapper.updateSalePriceStatus(editLog);

        //设置新增数据
        SysUser user = SecurityUtils.getLoginUser().getUser();
        salePriceLog.setCreateId(user.getUserId());
        salePriceLog.setCreateBy(user.getNickName());
        salePriceLog.setCreateTime(DateUtils.getNowDate());
        return salePriceLogMapper.insertSalePriceLog(salePriceLog);
    }

    /**
     * 批量删除预售价日志
     */
    @Override
    public int deleteSalePriceLogByIds(Long id)
    {
        SalePriceLog log = new SalePriceLog();
        log.setId(id);
        log.setStatus(2);
        return salePriceLogMapper.updateSalePriceLog(log);
    }
}
